package www.openteam.com.ui;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import www.openteam.com.R;
import www.openteam.com.bean.HomeTab;
import www.openteam.com.bean.HomeTab_E_HZ_RB;
import www.openteam.com.bean.OTBusiness;
import www.openteam.com.bean.OTPolicy;
import www.openteam.com.bean.OTPrdctnMngmnt;
import www.openteam.com.bean.QualityCtrl;
import www.openteam.com.bean.RealTime;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.ui.business.DeliverNoteFragment;
import www.openteam.com.widget.MyFragmentTabHost;

@SuppressLint("InflateParams")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class HomeActivity extends ActionBarActivity implements
        TabHost.OnTabChangeListener, View.OnClickListener,
        View.OnTouchListener {


    private static final String TAG = HomeActivity.class.getSimpleName();
    @Bind(android.R.id.tabhost)
    public MyFragmentTabHost mTabHost;
    @Bind(R.id.mDrawerLayout)
    DrawerLayout mDrawerLayout;
    @Bind(R.id.tv_content)
    TextView tv_content;
    @Bind(R.id.tv_versionName)
    TextView tv_versionName;
    private String mParam;
    RealTime[] realTime = RealTime.values();
    OTBusiness[] business = OTBusiness.values();
    OTPolicy[] policy = OTPolicy.values();
    OTPrdctnMngmnt[] prdctnMngmnts = OTPrdctnMngmnt.values();
    QualityCtrl[] qualityCtrls = QualityCtrl.values();

    ActionBar actionBar;

    private CharSequence mTitle;
    private ActionBarDrawerToggle mDrawerToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initView();
        mTabHost.setCurrentTab(0); // 选定当前的Tab
        mTabHost.setOnTabChangedListener(this); //Tab改变监听
    }

    @OnClick(R.id.btn_company)
    public void comAbstract() {
        Intent intent = new Intent(this, BriefIntroductionActivity.class);
        intent.putExtra("brief", "companyname");
        startActivity(intent);
    }

    @OnClick(R.id.btn_erp)
    public void erpAbstract() {
        Intent intent = new Intent(this, BriefIntroductionActivity.class);
        intent.putExtra("brief", "erpname");
        startActivity(intent);
    }

    @OnClick(R.id.btn_evaluate)
    public void evaAbstract() {
        Intent intent = new Intent(this, BriefIntroductionActivity.class);
        intent.putExtra("brief", "evaluatename");
        startActivity(intent);
    }

    public void initView() {

        // 初始化底部FragmentTabHost
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        if (android.os.Build.VERSION.SDK_INT > 10) {
            mTabHost.getTabWidget().setShowDividers(0);
        }
        initTabs(); //初始化Tabs
        tv_content.setText("简介");
    }

    private void initTabs() {
        Log.e(TAG, "initTabs: " + DetailedActivity.FACTORY);


        HomeTab[] tabs = HomeTab.values();  //Tabs的枚举类
//        HomeTab_E_HZ_RB[] tabs = HomeTab_E_HZ_RB.values();  //Tabs的枚举类

        final int size = tabs.length;  // Tabs的长度
        for (int i = 0; i < size; i++) {
            // 找到每一个枚举的Fragment对象
//            HomeTab_E_HZ_RB mainTab = tabs[i];
            HomeTab mainTab = tabs[i];

            // 1. 创建一个新的选项卡
            TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));

            //
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);

            //Tabs的图标
            Drawable drawable = this.getResources().getDrawable(
                    mainTab.getResIcon());
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            //Tabs的title
            title.setText(getString(mainTab.getResName()));

            tab.setIndicator(indicator);
            Bundle bundle = new Bundle();

//            if (DetailedActivity.FACTORY.equals("E_HZ_RB")||DetailedActivity.FACTORY.equals("ZZ")) {
//                //"E_HZ_RB";//ZZ//BASE
//                switch (i) {
//                    case 3:
//                        Log.d(TAG, "initTabs: 0");
//                        bundle.putInt("id", 3);
//                        bundle.putSerializable("realTime", realTime);
//                        break;
//                    case 2:
//                        Log.d(TAG, "initTabs: 2");
//                        bundle.putInt("id", 2);
//                        bundle.putSerializable("business", business);
//                        break;
//                    case 1:
//                        Log.d(TAG, "initTabs: 1");
//                        bundle.putInt("id", 1);
//                        bundle.putSerializable("policy", policy);
//                        break;
//                    case 0:
//                        Log.d(TAG, "initTabs: 3");
//                        bundle.putInt("id", 0);
//                        bundle.putSerializable("prdctnMngmnts", prdctnMngmnts);
//                        break;
//                    case 4:
//                        Log.d(TAG, "initTabs: 4");
//                        bundle.putInt("id", 4);
//                        bundle.putSerializable("qualityCtrls", qualityCtrls);
//                        break;
//                }
//            } else
            if (DetailedActivity.FACTORY.equals("E_HZ_RB")||DetailedActivity.FACTORY.equals("ZZ")) {
                switch (i) {
                    case 0:
                        Log.d(TAG, "initTabs: 0");
                        bundle.putInt("id", 0);
                        bundle.putSerializable("realTime", realTime);
                        break;
                    case 2:
                        Log.d(TAG, "initTabs: 2");
                        bundle.putInt("id", 2);
                        bundle.putSerializable("business", business);
                        break;
                    case 1:
                        Log.d(TAG, "initTabs: 1");
                        bundle.putInt("id", 1);
                        bundle.putSerializable("policy", policy);
                        break;
                    case 3:
                        Log.d(TAG, "initTabs: 3");
                        bundle.putInt("id", 3);
                        bundle.putSerializable("prdctnMngmnts", prdctnMngmnts);
                        break;
                    case 4:
                        Log.d(TAG, "initTabs: 4");
                        bundle.putInt("id", 4);
                        bundle.putSerializable("qualityCtrls", qualityCtrls);
                        break;
                }
            } else if (DetailedActivity.FACTORY.equals("BASE")) {
                switch (i) {
                    case 0:
                        Log.d(TAG, "initTabs: 0");
                        bundle.putInt("id", 0);
                        bundle.putSerializable("realTime", realTime);
                        break;
                    case 2:
                        Log.d(TAG, "initTabs: 2");
                        bundle.putInt("id", 2);
                        bundle.putSerializable("business", business);
                        break;
                    case 1:
                        Log.d(TAG, "initTabs: 1");
                        bundle.putInt("id", 1);
                        bundle.putSerializable("policy", policy);
                        break;
                    case 3:
                        Log.d(TAG, "initTabs: 3");
                        bundle.putInt("id", 3);
                        bundle.putSerializable("prdctnMngmnts", prdctnMngmnts);
                        break;
                    case 4:
                        Log.d(TAG, "initTabs: 4");
                        bundle.putInt("id", 4);
                        bundle.putSerializable("qualityCtrls", qualityCtrls);
                        break;
                }
            }


            mTitle = getString(tabs[0].getResName());
            // 2. 把新的选项卡添加到TabHost中
            mTabHost.addTab(tab, mainTab.getClz(), bundle);

            mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
        }
    }


    public void restoreActionBar() {
        actionBar = getSupportActionBar();
        // 设置显示为标准模式, 还有NAVIGATION_MODE_LIST列表模式, NAVIGATION_MODE_TABS选项卡模式. 参见ApiDemos
//        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        // 设置显示标题
//        actionBar.setLogo(R.drawable.logo);
        actionBar.setDisplayHomeAsUpEnabled(true);
//        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setHomeButtonEnabled(true);
//        actionBar.setDisplayShowHomeEnabled(true);
//        actionBar.setDisplayShowTitleEnabled(true);
        // 设置标题
        actionBar.setTitle(mTitle);
        mDrawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
//声明mDrawerToggle对象,其中R.string.open和R.string.close简单可以用"open"和"close"替代

        mDrawerToggle.syncState();//实现箭头和三条杠图案切换和抽屉拉合的同步
        mDrawerLayout.setDrawerListener(mDrawerToggle);//监听实现侧边栏的拉开和闭合,即抽屉drawer的闭合和打开
        //1.通过设置自定义内容VIew
//        actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);
//        actionBar.setDisplayShowHomeEnabled(true);
//        actionBar.setDisplayHomeAsUpEnabled(true);
//        View view = View.inflate(this, R.layout.layout_actionbar, null);
//        actionBar.setCustomView(view);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_activity_menu, menu);
        restoreActionBar();
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.search:
                ToastUtil.showToast(this, "Search");
                break;
            case R.id.share:
                ToastUtil.showToast(this, "share");
                break;
        }
        return mDrawerToggle.onOptionsItemSelected(item) | super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        //弹出对话框
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    @Override
    public void onTabChanged(String tabId) {
        mTitle = tabId;
        actionBar.setTitle(mTitle);
    }
}
